package com.mitcub.panda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mitcub.panda.base.BaseQuery;
import com.mitcub.panda.base.BaseServiceImpl;
import com.mitcub.panda.entity.Member;
import com.mitcub.panda.mapper.MemberMapper;
import com.mitcub.panda.query.AdminQuery;
import com.mitcub.panda.service.IMemberService;
import com.mitcub.panda.utils.RespResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * @项目名称: panda
 * @类名称: MemberServiceImpl
 * @类描述: 注册会员 服务实现类
 * @作者: tangloong
 * @创建时间: 2021/11/13 17:22
 */
@Service
public class MemberServiceImpl extends BaseServiceImpl<MemberMapper, Member> implements IMemberService {

    @Resource
    private MemberMapper memberMapper;

    /**
     * 获取数据列表
     * @param query 查询条件
     * @return
     */
    @Override
    public RespResult getList(BaseQuery query) {
        AdminQuery memberQuery = (AdminQuery) query;
        // 查询条件
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        // 人员姓名/手机号
        if (!StringUtils.isEmpty(memberQuery.getKeywords())) {
            queryWrapper.like("nickname", memberQuery.getKeywords())
                    .or().like("username", memberQuery.getKeywords())
                    .or().like("email", memberQuery.getKeywords())
                    .or().like("mobile", memberQuery.getKeywords());
        }
        // 状态：1正常 2禁用
        if (memberQuery.getStatus() != null) {
            queryWrapper.eq("status", memberQuery.getStatus());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("id");

        // 查询数据
        Page<Member> page = new Page<>(memberQuery.getPage(), memberQuery.getLimit());
        IPage<Member> data = memberMapper.selectPage(page, queryWrapper);
        return RespResult.success("操作成功", data.getRecords(),data.getTotal());
    }

}